'use strict';

angular
	.module('webDevApp', [
    	'ui.router'
    ])
  	.config(['$stateProvider','$locationProvider','$urlRouterProvider','$httpProvider', function($stateProvider, $locationProvider, $urlRouterProvider, $httpProvider){
  		$locationProvider.html5Mode(true);
  		$stateProvider.state('home',{
  			url:'/',
  			templateUrl: 'views/main.html',
  			controller: 'HomeController'
  		});
  		$stateProvider.state('cart',{
  			url:'/cart',
  			templateUrl: 'views/cart.html',
     		controller: 'CartController'
  		});
  		$stateProvider.state('orders',{
  			url:'/orders',
  			templateUrl: 'views/orders.html',
  		})
  		.state('orders.order', {
  			url:'/:order',
  			templateUrl: 'views/order.html',
  		});
  		$stateProvider.state('checkout',{
  			url:'/checkout',
  			templateUrl: 'views/checkout.html',
   			controller: 'CheckoutController'

  		});
  		$stateProvider.state('admin',{
  			url:'/admin',
  			templateUrl: 'views/admin.html',
  			controller:'AdminController',
  			resolve: {
  				orders: ['$http', function($http){
					return $http({ method: 'GET', url: 'allorders' }).success(function(data, status, headers, config) {
	                        console.log('Returned order data for admin');
	                        console.log(data.data);
	                    return data;
	                });
  				}]
  			}
  		})
  		$stateProvider.state('login',{
  			url:'/login',
  			templateUrl: 'views/login.html',
   			controller: 'LoginController'
  		});
  	}])
  	.run(['$rootScope',function($rootScope){
  		if(window.user != null)
  			if(typeof window.user !== 'object')
  				$rootScope.user = eval('('+window.user+')');
  			else
  				$rootScope.user = window.user;
  		else
  			$rootScope.user = null;

		// $rootScope.$on('$stateChangeStart', function(event, next, current) {
	 //    	if((next.name == 'cart' || next.name == 'checkout' || next.name == 'orders') && !$rootScope.user) {
	 //            $state.go('home');
	 //            event.preventDefault();
	 //        }else if(next.name == 'admin' && !$rootScope.user.role != 'admin'){
	 //        	$state.go('home');
	 //        	event.preventDefault();
	 //        }
  // 		});

  	}]);
